<template>
  <el-dialog
    :title="titleMap[mode]"
    v-model="visible"
    width="65%"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-form
      :model="form"
      :rules="rules"
      :disabled="mode == 'show'"
      ref="dialogForm"
      label-width="auto"
      label-position="right"
    >
      <el-form-item label="标题" prop="articleTitle">
        <el-input
          v-model="form.articleTitle"
          clearable
          placeholder="请输入条款标题"
        ></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="articleText" style="width: 100%">
        <sc-editor
          v-model="form.articleText"
          placeholder="请输入条款内容"
          :height="650"
          :disabled="mode == 'show'"
          :toolbar="toolbar"
        ></sc-editor>
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button
        v-if="mode != 'show'"
        type="primary"
        :loading="isSaveing"
        @click="submit()"
        >保 存</el-button
      >
    </template>
  </el-dialog>
</template>

<script>
import { defineAsyncComponent } from 'vue'
const scEditor = defineAsyncComponent(() => import('@/components/scEditor'))
export default {
  emits: ['success', 'closed'],
  data () {
    return {
      mode: "add",
      titleMap: {
        add: '新增',
        edit: '编辑',
        show: '查看'
      },
      visible: false,
      isSaveing: false,
      //表单数据
      form: {
        articleTitle: '',//标题
        articleText: '',//内容
        // isDefault: 0//是否默认： 0否 1是
      },
      //验证规则
      rules: {
        articleTitle: [
          { required: true, message: '请输入条款标题' }
        ],
        articleText: [
          { required: true, message: '请输入条款内容' }
        ],
      },
      toolbar: [
        'undo redo |  forecolor backcolor bold italic underline strikethrough link | blocks fontfamily fontsize | \
					alignleft aligncenter alignright alignjustify | outdent indent | numlist bullist | pagebreak | \
					image  table  preview | selectall fullscreen'
      ]
    }
  },
  components: {
    scEditor
  },
  mounted () {

  },
  methods: {
    //显示
    open (mode = 'add') {
      this.mode = mode
      this.visible = true
      return this
    },
    //表单提交方法
    submit () {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true
          let res = null
          if (this.mode == 'add') {
            res = await this.$API.contract.contractClause.addClause.post(this.form)
          } else if (this.mode == 'edit') {
            delete this.form.createTime
            res = await this.$API.contract.contractClause.updateClause.post(this.form)
          }
          this.isSaveing = false
          if (res.code == 200) {
            this.$emit('success', this.form, this.mode)
            this.visible = false
            this.$message.success("操作成功")
          }
        }
      })
    },
    //表单注入数据
    setData (data) {
      this.form = data
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
